package com.kanshu.ksgb.fastread.doudou.module.personal.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.dl7.recycler.helper.RecyclerViewHelper
import com.kanshu.ksgb.fastread.doudou.R
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity
import com.kanshu.ksgb.fastread.doudou.common.net.ResponseData
import com.kanshu.ksgb.fastread.doudou.common.net.SuperCallBack
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper
import com.kanshu.ksgb.fastread.doudou.common.view.EmptyLayout
import com.kanshu.ksgb.fastread.doudou.module.personal.adapter.CashRecordAdapter
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.CashRecordInfo
import com.kanshu.ksgb.fastread.doudou.module.personal.presenter.PersonCenterService
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.activity_encash_record.*
import retrofit2.Call

@Route(path = ARouterConfig.PERSONAL_CASH_RECORD)
class CashRecordActivity : BaseActivity() {

    companion object {
        fun actionStart(context: Context) {
            val intent = Intent(context, CashRecordActivity::class.java)
            context.startActivity(intent)
        }
    }

    lateinit var mAdapter: CashRecordAdapter
    var dataList = mutableListOf<CashRecordInfo>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        title = "我的零钱"
        setContentView(R.layout.activity_encash_record)
        init()
    }


    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
        MobclickAgent.onPageStart(javaClass.simpleName)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
        MobclickAgent.onPageEnd(javaClass.simpleName)
    }

    protected fun init() {
        mAdapter = CashRecordAdapter(activity, dataList)
        RecyclerViewHelper.initRecyclerViewV(activity, recyler_view, false, mAdapter)
        empty_layout.setRetryListener {
            showLoading(null)
            getData()
        }
        getData()
    }

    private fun getData() {
        RetrofitHelper.getInstance()
                .createService(PersonCenterService::class.java)
                .cashRecord
                .enqueue(object : SuperCallBack<List<CashRecordInfo>>() {
                    override fun onSuccess(call: Call<ResponseData<List<CashRecordInfo>>>, data: List<CashRecordInfo>?) {
                        if (data != null && data.isNotEmpty()) {
                            empty_layout?.visibility = View.GONE
                            dataList.clear()
                            dataList.addAll(data)
                            mAdapter.notifyDataSetChanged()
                        } else {
                            empty_layout?.emptyStatus = EmptyLayout.STATUS_NO_DATA
                        }
                    }

                    override fun onFailure(call: Call<ResponseData<List<CashRecordInfo>>>, t: Throwable) {
                        super.onFailure(call, t)
                        empty_layout?.emptyStatus = EmptyLayout.STATUS_NO_DATA
                    }
                })
    }
}